package com.orcamento.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import com.orcamento.entity.enums.Month;
import com.orcamento.entity.report.subreport.EventSubReport;
import com.orcamento.util.Money;

@Entity
@Table(name="Evento")
public class Event {
	
	@Id
	@SequenceGenerator(name="event_id", sequenceName="event_id")
	@GeneratedValue(generator="event_id", strategy=GenerationType.AUTO)
	@Column(name="IDEvento")
	private Long id;
	@Column(name="StNome", nullable=false)
	private String name;
	@Column(name="TxDescricao", nullable=true)
	private String description;
	@Column(name="BoAtivo", nullable=false)
	private Boolean active;

	@OneToOne(mappedBy="event")
	@Cascade({CascadeType.ALL})
	private Fund fund;
	
	@ManyToOne(targetEntity=Category.class)
	@JoinColumn(name="IDCategoria", referencedColumnName="IDCategoria", nullable=true)
	private Category category;
	@ManyToOne(targetEntity=Account.class)
	@JoinColumn(name="IDPatrimonio", referencedColumnName="IDPatrimonio", nullable=false)
	private Account account;
	
	@OneToMany(mappedBy="event")
	@Cascade({CascadeType.ALL})
	private List<Gain> gains = new ArrayList<Gain>();
	@OneToMany(mappedBy="event")
	@Cascade({CascadeType.ALL})
	private List<Spent> spents = new ArrayList<Spent>();

	public EventSubReport generateMonthReport(Month month, Integer year) {
		List<Gain> monthGains = new ArrayList<Gain>();
		List<Spent> monthSpents = new ArrayList<Spent>();
		for( Gain gain : gains ) {
			if( gain.isIn(year, month) ) {
				monthGains.add( gain );
			}
		}
		
		for( Spent spent : spents ) {
			if( spent.isIn(year, month) && ! spent.isCreditCardInvoicePayment() ) {
				monthSpents.add( spent );
			}
		}
		
		return new EventSubReport(month, year, monthGains, monthSpents);
	}
	
	public Money getTotalGain(Month month, Integer year) {
		Money total = new Money();
		for(Gain gain : gains) {
			if( gain.isIn(year, month) ) {
				total = total.plus(gain.getValue());
			}
		}
		return total;
	}
	
	public Money getTotalSpents(Month month, Integer year) {
		Money total = new Money();
		for(Spent spent : spents) {
			if( ! spent.isCreditCardInvoicePayment() && spent.isIn(year, month) ) {
				total = total.plus(spent.getValue());
			}
		}
		return total;
	}
	
	public Money getTotalCreditCardSpents(Month month, Integer year) {
		Money total = new Money();
		for(Spent spent : spents) {
			if( ! spent.isCreditCardInvoicePayment() && spent.isCreditCardPayment() && spent.isIn(year, month) ) {
				total = total.plus(spent.getValue());
			}
		}
		return total;
	}
	
	public Money getTotalCashSpents(Month month, Integer year) {
		Money total = new Money();
		for(Spent spent : spents) {
			if( ! spent.isCreditCardInvoicePayment() && spent.isCashPayment() && spent.isIn(year, month) ) {
				total = total.plus(spent.getValue());
			}
		}
		return total;
	}
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Boolean getActive() {
		return active;
	}
	public void setActive(Boolean active) {
		this.active = active;
	}
	public List<Gain> getGains() {
		return gains;
	}
	public void setGains(List<Gain> gains) {
		this.gains = gains;
	}
	public List<Spent> getSpents() {
		return spents;
	}
	public void setSpents(List<Spent> spents) {
		this.spents = spents;
	}
	public Fund getFund() {
		return fund;
	}
	public void setFund(Fund fund) {
		this.fund = fund;
	}
	public Category getCategory() {
		return category;
	}
	public void setCategory(Category category) {
		this.category = category;
	}

	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

}
